<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 下面就是树形控件内容了 -->
        <!-- data表示树形的数据 -->
        <!-- props表示展示的规则  { label:'xxx', children:'xx' }  label表示要展示的文字的字段，children表示子级元素存放的字段名称 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools slot-scope="{ data }" :tree-node="data" @editDepts="editDepts" @addDepts="addDepts" @delDepts="getDepartments" />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <add-dept ref="add-dept" :tree-node="node" @addDepts="getDepartments" />

  </div>
</template>
<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { treeTools, AddDept },
  data() {
    return {
      company: { }, // 就是头部的数据结构
      departs: [],
      defaultProps: {
        label: 'name'
      },
      node: {} // 点击行的数据
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取部门数据
    async getDepartments() {
      const { companyName, depts } = await getDepartments()
      this.company = { name: companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(depts, '')
    },
    // 监听 tree-tools 里面添加
    addDepts(node) {
      this.node = node
      this.$refs['add-dept'].showDialog = true
    },
    // 监听 tree-tools 里面修改
    editDepts(node) {
      this.node = node
      this.$refs['add-dept'].showDialog = true
      this.$refs['add-dept'].getDepartDetail(node.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>

